#include "myselect.h"
sqlite3 *db;
void my_select(char *username,int socketfd)
{


	int permission=get_permission(username);
	if(permission==1){
		printf("guanliyuan\n");
		//send select menu
		int size;
		char senbuf[100]="***1.get_all_mesg 2.get_all_name 3. one_user_mesg***\n";
		size=strlen(senbuf)+1;
		write(socketfd,&size,sizeof(int));
		write(socketfd,senbuf,size);
		read(socketfd,&size,sizeof(int));
		read(socketfd,senbuf,size);
		switch(atoi(senbuf)){
			case 1:	
				{//1,all mesg
					printf("use 1\n");
					char *buf;
					get_all_msg(&buf);
					strcat(buf,"\n*****input any key to continue*****\n");
					size =strlen(buf)+1;
					write(socketfd,&size,sizeof(int));
					write(socketfd,buf,size);
					free_all_msg(buf);
					read(socketfd,&size,sizeof(int));
					char unusefulbuf[100]={0};
					read(socketfd,unusefulbuf,size);
				}
					break;
			case 2:
					{
					//2,all name
					printf("use 2\n");
					char *buf;
					get_all_name(&buf);
					strcat(buf,"\n*****input any key to continue*****\n");
					size=strlen(buf)+1;
					write(socketfd,&size,sizeof(int));
					write(socketfd,buf,size);
					free_all_name(buf);
					read(socketfd,&size,sizeof(int));
					char unusefulbuf[100]={0};
					read(socketfd,unusefulbuf,size);
					}
					break;
			case 3:
					{
					//3,one user mesg
					printf("use 3\n");
					char sendbuf1[100]={0};
					sprintf(sendbuf1,"intput the name what you want to know\n");
					size=strlen(sendbuf1)+1;
					write(socketfd,&size,sizeof(int));
					write(socketfd,&sendbuf1,size);
					read(socketfd,&size,sizeof(int));
					read(socketfd,&sendbuf1,size);
					char name[20]={0};
					strncpy(name,sendbuf1,size);
					struct mem_info mem;
					if(0!=select_table_byname(name, &mem))
					{
					char buf[200]={0};
					to_string(buf,&mem);
					strcat(buf,"\n*****input any key to continue*****\n");
					size=strlen(buf)+1;
					write(socketfd,&size,sizeof(int));
					write(socketfd,buf,size);
					}
					else{
					char buf[100]={0};
					strcat(buf,"\n***** no the user *****\n******input any key to continue*****");
					size=strlen(buf)+1;
					write(socketfd,&size,sizeof(int));
					write(socketfd,buf,size);
					}
					read(socketfd,&size,sizeof(int));
					char unusefulbuf[100]={0};
					read(socketfd,unusefulbuf,size);
					}
					break;
			default:
					{
					char sendbuf1[100]={0};
					sprintf(sendbuf1,"***input error***\n");
  					strcat(sendbuf1,"\n*****input any key to continue*****\n");
					size=strlen(sendbuf1)+1;
					write(socketfd,&size,sizeof(int));
					write(socketfd,sendbuf1,size);
					read(socketfd,&size,sizeof(int));
					char unusefulbuf[100]={0};
					read(socketfd,unusefulbuf,size);

					}
					break;
		}
	}
	else{
		printf("putong user\n");
		char senbuf1[100]="***1.get_all_name 2.get_one_usermsg***\n";
		int size;
		size=strlen(senbuf1)+1;
		write(socketfd,&size,sizeof(int));
		write(socketfd,senbuf1,size);
		read(socketfd,&size,sizeof(int));
		read(socketfd,senbuf1,size);
		switch(atoi(senbuf1)){
			case 1:
				{
					printf("use 1\n");
					//1,get_all_name
					char *buf;
					get_all_name(&buf);
					strcat(buf,"\n*****input any key to continue*****\n");
					size=strlen(buf)+1;
					write(socketfd,&size,sizeof(int));
					write(socketfd,buf,size);
					free_all_name(buf);
					read(socketfd,&size,sizeof(int));
					char unusefulbuf[100]={0};
					read(socketfd,unusefulbuf,size);

				}
					break;
			case 2:
					{
					printf("use 2\n");
					//2,one user some msg
					char sendbuf2[100]={0};
					sprintf(sendbuf2,"intput the name what you want to know\n");
					size=strlen(sendbuf2)+1;
					write(socketfd,&size,sizeof(int));
					write(socketfd,&sendbuf2,size);
					read(socketfd,&size,sizeof(int));
					read(socketfd,&sendbuf2,size);
					char name[20]={0};
					strncpy(name,sendbuf2,size);
					struct mem_info mem;
					if(0!=select_table_byname(name, &mem))
					{
					char buf[200]={0};
					get_usfulmsf(buf,&mem);
					strcat(buf,"\n*****input any key to continue*****\n");
					size=strlen(buf)+1;
					write(socketfd,&size,sizeof(int));
					write(socketfd,buf,size);
					}
					else{
						char buf[100]={0};
						strcat(buf,"\n*****no the user****\n*****input any key to continue*****\n");
						size=strlen(buf)+1;
						write(socketfd,&size,sizeof(int));
						write(socketfd,buf,size);
					}
					read(socketfd,&size,sizeof(int));
					char unusefulbuf[100]={0};
					read(socketfd,unusefulbuf,size);

					}
					break;
			default:
					{
					char sendbuf2[100]={0};
					sprintf(sendbuf2,"***input error***\n");
					strcat(sendbuf2,"\n*****input any key to continue*****\n");

					size=strlen(sendbuf2)+1;
					write(socketfd,&size,sizeof(int));
					write(socketfd,sendbuf2,size);
					read(socketfd,&size,sizeof(int));
					char unusefulbuf[100]={0};
					read(socketfd,unusefulbuf,size);

					}
					break;
		}
	}	
}

int get_permission(char *username)
{
	sqlite3 *db;
	if(sqlite3_open("./my.db",&db)!= SQLITE_OK)
	{
		printf("error : %s\n",sqlite3_errmsg(db));
	}
	char **resultp;
	int nrow,ncolum;
	char *errmsg;
	char sqbuf[100]={0};
	sprintf(sqbuf,"select permission from userpasswd where name=\"%s\";",username);
	if(0 != sqlite3_get_table(db,sqbuf,&resultp,&nrow,&ncolum,&errmsg))
	{
		printf("select error: %s\n",errmsg);
	}
	if(*resultp[1]=='1')
	{
		sqlite3_close(db);
		return 1;
	}
	sqlite3_close(db);
	return 0;
}
static int get_usfulmsf(char *buf,struct mem_info *mem)
{
	char a[10]={0};
	sprintf(a,"%d ",mem->mem_no);
	strcat(buf,a);
	strcat(strcat(buf,mem->name)," ");
	strcat(strcat(buf,mem->address)," ");
	strcat(strcat(buf,mem->phone)," ");
	char b[10]={0};
	sprintf(b,"%d ",mem->age);
	strcat(buf,b);
	return 0;

}
static int to_string(char *buf,struct mem_info *mem)
{
	char a[10]={0};
	sprintf(a,"%d ",mem->mem_no);
	strcat(buf,a);
	strcat(strcat(buf,mem->name)," ");
	strcat(strcat(buf,mem->passwd)," ");
	strcat(strcat(buf,mem->address)," ");
	strcat(strcat(buf,mem->phone)," ");
	char b[10]={0};
	sprintf(b,"%d ",mem->age);
	strcat(buf,b);
	char c[10]={0};
	sprintf(c,"%d ",mem->salary);
	strcat(buf,c);
	return 0;
}
